package com.blog.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.UUID;


/** 
 * @author ant 
 * @date 创建时间：2017年3月27日 下午2:24:37  
 */
public class FileUtil {
	
	/**
	 * @Description: 拼接html 
	 * @param content 文章内容
	 * @param title html的标题
	 * @return String
	 */
	private static String getHtml(String content) {
		StringBuffer sb = new StringBuffer();
		sb.append("<!doctype html>\r\n");
		sb.append("<html>\r\n");
		sb.append("<head>\r\n");
		sb.append("<meta charset='UTF-8'>\r\n");
		sb.append("<title>模板</title>\r\n");
		sb.append("</head>\r\n");
		sb.append("<body>\r\n");
		sb.append(content);
		sb.append("</body>\r\n");
		sb.append("</html>\r\n");
		return sb.toString();		
	}
	
	/**
	 * @Description: 将html写入到本地的html文件中
	 * @param FileName html的名字
	 * @param content 内容
	 * @param path 本地的路径
	 * @return File
	 * @throws IOException 
	 */
	public static File outputFile(String fileName,String articleContent,String path) throws IOException {
		String content = getHtml(articleContent);
		File filePath = new File(path);
		if (!filePath.exists()) {
			filePath.mkdirs();
		}
		File fileHtml = new File(filePath, fileName);
		if (!fileHtml.exists()) {
			fileHtml.createNewFile();
		}
		try(Writer writer = new OutputStreamWriter(new FileOutputStream(fileHtml),"UTF-8")) {				
			writer.write(content);
		} 
		return fileHtml;
	}

	public static String getFileName() {
		String name =  UUID.randomUUID().toString();
		String fileName = name.replaceAll("-","");
		return fileName;
	}
}
